<?php
// Author: dolphinigle
// 28 March 2012
include 'config/koneksi.php';

$user = $_GET[username];

if ($_GET[type] == 'likes') {
  $query = "SELECT COUNT(likes.id_berita) AS cnt FROM likes " .
           "WHERE id_berita='" . $_GET[id] .
           "' AND username='" . $user . "'";
} elseif ($_GET[type] == 'dislike') {
  $query = "SELECT COUNT(dislike.id_berita) AS cnt FROM dislike " .
           "WHERE id_berita='" . $_GET[id] .
           "' AND username='" . $user . "'";
} else {
  exit('UNRECOGNIZED TYPE PARAMETER');
}

$sql_answer = mysql_query($query);
$ans = mysql_fetch_array($sql_answer);

if ($ans[cnt] == 0) {
  // Add it
  echo 'INSERT';
  $result = mysql_query("INSERT INTO " . $_GET[type] .
                        "(id_berita, username) VALUES('" .
                        $_GET[id] . "', '" . $user . "')");
} else {
  // Remove it
  echo 'DELETE';
  $result = mysql_query("DELETE FROM " . $_GET[type] .
                        " WHERE id_berita='" . $_GET[id] . "' AND" .
                        " username='" . $user . "'");
}

 echo $result;
?>
